// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: H Crush Girder Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 52 : TYPE_HCRUSHGIRDER

// Function declarations
reserve function HCrushGirder_DebugDraw
reserve function HCrushGirder_DebugSpawn
reserve function HCrushGirder_PushMonika

// Static Values

// Tables

function HCrushGirder_DebugDraw
	DrawSprite(0)
end function


function HCrushGirder_DebugSpawn
	CreateTempObject(TypeName[H Crush Girder], 0, object.xpos, object.ypos)
	object[tempObjectPos].value1 = object.xpos
	object[tempObjectPos].value2 = object.ypos
end function


function HCrushGirder_PushMonika
	if object[currentPlayer].state == PlayerObject_HandleMonikaTeleport
		if object[0].value1 >= 14
			BoxCollisionTest(C_TOUCH, object.entityPos, -96, -24, 96, 24, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				if object[currentPlayer].ypos > object.ypos
					temp0 = 1737
					temp0 <<= 16
					object[currentPlayer].xpos = temp0

					object[currentPlayer].iypos = object[currentPlayer].collisionBottom
					FlipSign(object[currentPlayer].ypos)
					object[currentPlayer].ypos += 0x400000
					object[currentPlayer].ypos += object.ypos
					if object[currentPlayer].yvel < 0
						object[currentPlayer].yvel = 0
					end if
				else
					object[currentPlayer].iypos = object[currentPlayer].collisionTop
					FlipSign(object[currentPlayer].ypos)
					object[currentPlayer].ypos -= 0x400000
					object[currentPlayer].ypos += object.ypos
					if object[currentPlayer].yvel > 0
						object[currentPlayer].yvel = 0
					end if
				end if		
			end if	
		end if	
	end if	
end function

event ObjectMain
	switch object.state
	case 0
		object.value0++
		if object.value0 == 96
			object.value0 = 6
			object.value3 = 1
			object.state = 4
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			CheckEqual(object[currentPlayer].state, PlayerObject_HandleMonikaTeleport)
			temp7 = checkResult
			CheckLower(object[0].value1, 20)
			temp7 &= checkResult
			if temp7 == 0					
				BoxCollisionTest(C_BOX, object.entityPos, -96, -24, 96, 24, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				switch checkResult
				case 1
					object[currentPlayer].xpos += 0x10000
					break
				case 2
					object[currentPlayer].pushing = 0
					break
				end switch
			end if	
			CallFunction(HCrushGirder_PushMonika)
		next
		object.xpos += 0x10000
		break
	case 1
		object.value0++
		if object.value0 == 48
			object.value0 = 6
			object.value3 = 2
			object.state = 4
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			CheckEqual(object[currentPlayer].state, PlayerObject_HandleMonikaTeleport)
			temp7 = checkResult
			CheckLower(object[0].value1, 20)
			temp7 &= checkResult
			if temp7 == 0					
				BoxCollisionTest(C_BOX, object.entityPos, -96, -24, 96, 24, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				switch checkResult
				case 1
					object[currentPlayer].ypos += 0x10000
					break
				case 4
					if object[currentPlayer].gravity == GRAVITY_GROUND
						CallFunction(PlayerObject_Kill)
					end if
					break
				end switch
			end if	
			CallFunction(HCrushGirder_PushMonika)
		next
		object.ypos += 0x10000
		break
	case 2
		object.value0++
		if object.value0 == 96
			object.value0 = 6
			object.value3 = 3
			object.state = 4
		end if
		object.ypos -= 0x4000
		object.xpos -= 0x10000
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			CheckEqual(object[currentPlayer].state, PlayerObject_HandleMonikaTeleport)
			temp7 = checkResult
			CheckLower(object[0].value1, 20)
			temp7 &= checkResult
			if temp7 == 0					
				BoxCollisionTest(C_BOX, object.entityPos, -96, -24, 96, 24, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				switch checkResult
				case 1
					object[currentPlayer].xpos -= 0x10000
					break
				case 4
					break
				end switch
			end if	
			CallFunction(HCrushGirder_PushMonika)
		next
		break
	case 3
		object.value0++
		if object.value0 == 24
			object.value0 = 6
			object.value3 = 0
			object.state = 4
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			CheckEqual(object[currentPlayer].state, PlayerObject_HandleMonikaTeleport)
			temp7 = checkResult
			CheckLower(object[0].value1, 20)
			temp7 &= checkResult
			if temp7 == 0					
				BoxCollisionTest(C_BOX, object.entityPos, -96, -24, 96, 24, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				switch checkResult
				case 1
					object[currentPlayer].ypos -= 0x10000
					break
				case 4
					break
				end switch
			end if	
			CallFunction(HCrushGirder_PushMonika)
		next
		object.ypos -= 0x10000
		break
	case 4
		if object.value0 > 0
			object.value0--
		else
			object.state = object.value3
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			CheckEqual(object[currentPlayer].state, PlayerObject_HandleMonikaTeleport)
			temp7 = checkResult
			CheckLower(object[0].value1, 20)
			temp7 &= checkResult
			if temp7 == 0					
				BoxCollisionTest(C_BOX, object.entityPos, -96, -24, 96, 24, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				if checkResult == 4
					if object[currentPlayer].gravity == GRAVITY_GROUND
						CallFunction(PlayerObject_Kill)
					end if
				end if
			end if	
			CallFunction(HCrushGirder_PushMonika)
		next
		break
	end switch
end event


event ObjectDraw
	DrawSprite(0)
end event


event ObjectStartup
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-96, -24, 192, 48, 319, 1)
	foreach (TypeName[H Crush Girder], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].value1 = object[arrayPos0].xpos
		object[arrayPos0].value2 = object[arrayPos0].ypos
	next
	SetTableValue(TypeName[H Crush Girder], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(HCrushGirder_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(HCrushGirder_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
